package com.bjsxt.softrc.test.dbpool;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.bjsxt.softrc.dao.DBUtil;

public class MyPool {
	private List<Connection> pool;
	
	private static final int POOL_MAX_SIZE=100;
	private static final int POOL_MIN_SIZE=50;

	public MyPool(){
		init();
	}
	
	private void init(){
		if(pool==null){
			pool = new ArrayList<Connection>();
		}
		for(int i=0;i<POOL_MIN_SIZE;i++){
			Connection conn = DBUtil.getMysqlConn();
			pool.add(conn);
		}
	}
	
	public Connection getConn(){
		Connection conn = pool.get(pool.size()-1);
		pool.remove(conn);
		
    	InvocationHandler ih = new ProxyInvocation(conn,this);
    	
    	Class c = conn.getClass();
		return (Connection) Proxy.newProxyInstance(c.getClassLoader(),c.getInterfaces(),ih);
	}
	
	public void closeConn(Connection c){
		pool.add(c);
	}
	
	
}
